package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.${capitalize};
import cn.wolfcode.wolf2w.query.${capitalize}QueryObject;
import cn.wolfcode.wolf2w.service.I${capitalize}Service;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("${uncapitalize}")
public class ${capitalize}Controller {

    @Autowired
    private I${capitalize}Service ${uncapitalize}Service;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ${capitalize}QueryObject qo) {
        Page<${capitalize}> list = ${uncapitalize}Service.query(qo);
        model.addAttribute("page", list);
        return "${uncapitalize}/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(${capitalize} ${uncapitalize}) {
        if(!StringUtils.hasLength(${uncapitalize}.getId())) {
            ${uncapitalize}Service.save(${uncapitalize});
        } else {
            ${uncapitalize}Service.update(${uncapitalize});
        }
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        ${uncapitalize}Service.delete(id);
        return JsonResult.success();
    }

}
